#include"head.h"

int cdServe(int NetFd)
{
    char protocol[100] = { 0 };
    char *agv[3];

    printf("recv ing\n");
    recv(NetFd,protocol,sizeof(protocol),0);

    getToken(protocol,agv);
    char *path = agv[1];
    char *param = agv[2];

    printf("path:%s,param:%s\n",agv[1],agv[2]);

    DIR *pdir = opendir(path);
    char pos[10] = { 0 };

    struct dirent* pdirent;

    while((pdirent = readdir(pdir)) != NULL)
    {
        if(strcmp(param, pdirent->d_name) == 0)
        {
            strcpy(pos,"成功");
            printf("cd %s 成功\n",path);
            break;
        }
    }

    send(NetFd,pos,sizeof(pos),0);

    return 0;
}

int mkdirServe(const int NetFd)
{
    // 网络键值对切割接收数组
    char *agv[3] = { 0 };
    // 网络键值对接收数组
    char protocol[100] = { 0 };

    recv(NetFd,protocol,sizeof(protocol),0);

    getToken(protocol,agv);

    // 组合当前工作路径和命令参数，组成绝对路径
    char path[100] = { 0 };
    strcat(path,agv[1]);
    strcat(path,"/");
    strcat(path,agv[2]);

    int err = mkdir(path,0777);
    if(err)
    {
        printf("创建失败！\n");
        return -1;
    }
    char respon[] = "创建成功\n";
    send(NetFd,respon,sizeof(respon),0);

    return 0;
}


int getToken(char *protocol,char **agv)
{
    agv[0] = strtok(protocol,"&");
    agv[1] = strtok(NULL,"&");
    agv[2] = strtok(NULL,"&");

    for(int i = 0;i < 3;i++)
        for(char *p = agv[i];*p != '\0';p++)
            if(*p == '=')
                agv[i] = p + 1;

    return 0;
}
